Passed
Push — master ( 1ff847...a1e431 )
by
unknown
02:16
created

CreateContactAction   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A index 0 29 2
1
import {
2
  Body,
3
  Post,
4
  Controller,
5
  Inject,
6
  BadRequestException,
7
  UseGuards
8
} from '@nestjs/common';
9
import { AuthGuard } from '@nestjs/passport';
10
import { ApiTags, ApiBearerAuth, ApiOperation } from '@nestjs/swagger';
11
import { ICommandBus } from 'src/Application/ICommandBus';
12
import { ContactDTO } from '../DTO/ContactDTO';
13
import { RolesGuard } from 'src/Infrastructure/HumanResource/User/Security/RolesGuard';
14
import { UserRole } from 'src/Domain/HumanResource/User/User.entity';
15
import { Roles } from 'src/Infrastructure/HumanResource/User/Decorator/Roles';
16
import { CreateContactCommand } from 'src/Application/Contact/Command/CreateContactCommand';
17
18
@Controller('contacts')
19
@ApiTags('Contact')
20
@ApiBearerAuth()
21
@UseGuards(AuthGuard('bearer'), RolesGuard)
22
export class CreateContactAction {
23
  constructor(
24
    @Inject('ICommandBus')
25
    private readonly commandBus: ICommandBus
26
  ) {}
27
28
  @Post()
29
  @Roles(UserRole.COOPERATOR, UserRole.EMPLOYEE)
30
  @ApiOperation({ summary: 'Create new contact' })
31
  public async index(@Body() contactDto: ContactDTO) {
32
    const {
33
      firstName,
34
      lastName,
35
      company,
36
      email,
37
      phoneNumber,
38
      notes
39
    } = contactDto;
40
41
    try {
42
      const id = await this.commandBus.execute(
43
        new CreateContactCommand(
44
          firstName,
45
          lastName,
46
          company,
47
          email,
48
          phoneNumber,
49
          notes
50
        )
51
      );
52
53
      return { id };
54
    } catch (e) {
55
      throw new BadRequestException(e.message);
56
    }
57
  }
58
}
59